package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.util.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.Page;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("setmeal")
@Slf4j
public class SetMealController {

    @DubboReference
    private SetmealService setmealService;
    @Autowired
    private QiniuUtils qiniuUtils;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Author: 15041
     * @Date: 14:51 2021/1/8
     * @Parms: [queryPageBean]
     * @ReturnType: com.itheima.health.entity.Result
     * Description: 套餐-分页查询
     */
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("套餐-套餐分页查询controller层传参：{}", queryPageBean);
        PageResult page = setmealService.findPage(queryPageBean);
        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, page);
    }

    /**
     * @Author: 15041
     * @Date: 14:51 2021/1/8
     * @Parms: [multipartFile]
     * @ReturnType: com.itheima.health.entity.Result
     * Description: 套餐-新增套餐-上传图片
     */
    @PostMapping("upload")
    public Result upload(@RequestParam("imgFile") MultipartFile file) {
        log.info("套餐-上传图片controller层传参：{}", file);
        try {
            //必须保证文件名称唯一性  uuid+原名称
            //获取图片原名称
            String originalFilename = file.getOriginalFilename();
            //拼接文件名称 获取uuid 转换成字符串 去除中间的 - 拼接图片原名称
            String fileName = UUID.randomUUID().toString().replace("-", "") + "_" + originalFilename;

            //优化图片上传
            //拼接时间戳 时间戳_uuid_原始文件名
            fileName = System.currentTimeMillis() + "_" + fileName;
            log.info("套餐-文件上传最终拼接名字：{}",fileName);
            //存入redis中
            redisTemplate.boundSetOps(RedisConst.SETMEAL_PIC_RESOURCES).add(fileName);

            //上传完毕，返回图片网址，让新增页面回显图片
            qiniuUtils.upload2Qiniu(file.getInputStream(), fileName);
            //上传完毕返回文件网址  获取网址+文件名称
            String imageUrl = qiniuUtils.getUrlPrefix() + fileName;


            log.info("文件上传后返回的网址是：{}" + imageUrl);
            return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS, imageUrl);
        } catch (IOException e) {
            log.error("套餐-上传图片失败：{}", e.getMessage());
            return new Result(false, MessageConst.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * @Author: 15041
     * @Date: 16:58 2021/1/8
     * @Parms: [setmeal, checkgroupIds]
     * @ReturnType: com.itheima.health.entity.Result
     * Description: 套餐-新增套餐
     */
    @PostMapping("add")
    @Transactional(rollbackFor = Exception.class)//添加事务 如果出现异常回滚
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("套餐-新增套餐controller层传参,setmeal：{},checkgroupIds ：{}", setmeal, checkgroupIds);
        //获取图片名称 进行截取修改成图片名称
        String img = setmeal.getImg();
        //判断是否有图片名称
        if (StringUtils.isNotEmpty(img)) {
            //截取域名 去掉域名
            String replace = img.replace(qiniuUtils.getUrlPrefix(), "");
            setmeal.setImg(replace);
        }
        setmealService.add(setmeal, checkgroupIds);
        return new Result(true, MessageConst.ADD_SETMEAL_SUCCESS);
    }

    /**
     * @Author: 15041
     * @Date: 18:26 2021/1/18
     * @Parms: [id]
     * @ReturnType: com.itheima.health.entity.Result
     *Description: 根据id查询套餐
    */
    @GetMapping("findById")
    public Result findById(Integer id) {
        log.info("套餐-根据id查询套餐数据传参：{}", id);
        Setmeal list = setmealService.findById(id);
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, list);
    }

    /**
     * @Author: 15041
     * @Date: 18:26 2021/1/18
     * @Parms: [setmeal, checkgroupIds]
     * @ReturnType: com.itheima.health.entity.Result
     *Description: 修改套餐
    */
    @PostMapping("update")
    @Transactional(rollbackFor = Exception.class)//添加事务 如果出现异常回滚
    public Result update(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("套餐-修改套餐数据传参controller层传参 setmeal：{},checkgroupIds :{}", setmeal, checkgroupIds);
        setmealService.update(setmeal, checkgroupIds);
        return new Result(true, MessageConst.EDIT_SETMEAL_SUCCESS);
    }

    /**
     * @Author: 15041
     * @Date: 18:25 2021/1/18
     * @Parms: [id]
     * @ReturnType: com.itheima.health.entity.Result
     *Description: 删除套餐
    */
    @PostMapping("delete")
    @Transactional(rollbackFor = Exception.class)//添加事务 如果出现异常回滚
    public Result delete(Integer id) {
        log.info("套餐-删除套餐传参：{}", id);
        setmealService.delete(id);
        return new Result(true, MessageConst.DELETE_SETMEAL_SUCCESS);
    }

}
